package com.colin.app.activity.other.touch;

import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.colin.app.R;
import com.colin.app.activity.other.OtherContract;
import com.colin.app.activity.other.OtherPresenterImpl;
import com.colin.app.app.AppActivity;
import com.colin.app.bean.DemoBean;

public class TouchViewActivity extends AppActivity implements OtherContract.OtherView, View.OnClickListener {
    private OtherContract.OtherPresenter mOtherPresenter = null;
    private View view_touch_me;
    private TextView text_touch_view_result;
    private Handler scaleHandler = new Handler();

    @Override
    protected void onDestroy() {
        scaleHandler = null;
        if (null != mOtherPresenter) {
            mOtherPresenter.destroy();
            mOtherPresenter = null;
        }
        super.onDestroy();
    }

    /**
     * 默认状体获取View的状态和位置信息为0
     */
    @Override
    protected void onResume() {
        super.onResume();
        scaleHandler.post(new Runnable() {
            @Override
            public void run() {
                //具体的处理函数，里面有获取X,Y，宽高等信息，之前是直接在onResume中被调用。
                mOtherPresenter.setViewData(view_touch_me, text_touch_view_result);
            }
        });

    }

    @Override
    public void initPresenter() {
        DemoBean demoBean = getIntent().getExtras().getParcelable("clazz");
        mOtherPresenter = new OtherPresenterImpl(this, this, demoBean);
    }

    @Override
    public int initViewLayoutID() {
        return R.layout.activity_touch_view;
    }

    @Override
    public void initView() {
        this.view_touch_me = this.findViewById(R.id.view_touch_me);
        this.text_touch_view_result = (TextView) this.findViewById(R.id.text_touch_view_result);
    }


    @Override
    public void initData() {
        mOtherPresenter.setData();
    }

    @Override
    public void initListener() {
        this.findViewById(R.id.button_view_angle).setOnClickListener(this);
        this.findViewById(R.id.button_view_radian).setOnClickListener(this);
        view_touch_me.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mOtherPresenter.touchView(v, event);
                return true;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_view_angle:
                mOtherPresenter.alertAngle();
                break;
            case R.id.button_view_radian:
                mOtherPresenter.alertRadian();
                break;
        }
    }
}
